package com.lujianfei.powerinput.base;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.lujianfei.powerinput.utils.CastUtils;

public class ViewHolder {
    private final View mConverView;
    private static int mPosition;
    private final SparseArray<View> mViewSparseArray;

    private ViewHolder(Context context, int layout, ViewGroup parent, int position) {
        mConverView = LayoutInflater.from(context).inflate(layout, parent, false);
        mViewSparseArray = new SparseArray<>();
        mConverView.setTag(this);
        mPosition = position;
    }

    /**
     * 可以理解成单例吧，获取一个viewholder
     *
     * @param context
     * @param converView
     * @param layout
     * @param parent
     * @param position
     * @return
     */
    public static ViewHolder getViewHolder(Context context, View converView, int layout,
                                           ViewGroup parent, int position) {
        if (converView == null) {            //注意这里，其实跟baseadapter的判断是一致的，其中settag也是在viewholder里
            return new ViewHolder(context, layout, parent, position);
        } else {
            ViewHolder holder = (ViewHolder) converView.getTag();
            mPosition = position;
            return holder;
        }
    }

    /**
     * 返回一个viewholder
     *
     * @return
     */
    public View getConverView() {
        return mConverView;
    }

    /**
     * key-value 通过sparseArray来获取view，属性不同，这里用一个泛型来实现
     *
     * @param viewid
     * @param <T>
     * @return
     */
    public <T extends View> T getView(int viewid) {
        View view = mViewSparseArray.get(viewid);
        if (view == null) {
            view = mConverView.findViewById(viewid);
            mViewSparseArray.put(viewid, view);
        }
        return CastUtils.cast(view);
    }

    /**
     * 设置text
     *
     * @param viewid
     * @param msg
     */
    public void setText(int viewid, String msg) {
        TextView tv = (TextView) mConverView.findViewById(viewid);
        tv.setVisibility(View.VISIBLE);
        tv.setText(msg);
    }

    /**
     * 设置drawable
     *
     * @param viewid
     * @param drawable
     */
    public void setDrawable(int viewid, Drawable drawable) {
        ImageView iv = (ImageView) mConverView.findViewById(viewid);
        iv.setImageDrawable(drawable);
    }

    /**
     * 设置checkbox
     *
     * @param viewid
     * @param status
     */
    public void setCheckbox(int viewid, boolean status) {
        CheckBox cb = (CheckBox) mConverView.findViewById(viewid);
        cb.setVisibility(View.VISIBLE);
        cb.setChecked(status);
    }

    /**
     * 设置itemview 的背景
     *
     * @param viewid
     */
    public void setItemBackground(int viewid, int resourid) {
        View view = mConverView.findViewById(viewid);
        //Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),resourid);
        view.setBackgroundResource(resourid);
    }
}